import { window } from "vscode"

/**
 * 根据颜色代码生成装饰器
 */
 export function createDecoration  (code) {
    let dro = {}
    if (/\d{3}/.test(code)) {
        dro = createDecorationOptionVt100(code)
    } else if (/g([01][0-9]|2[0-3])/i.test(code)) {
        dro = createDecorationOptionGray(code)
    } else if (/[fb][0-9a-f]{3}([0-9a-f]{3})?/i.test(code)) {
        dro = createDecorationOptionTrueColor(code)
    } else if (/[a-f]{3}/i.test(code)) {
        dro = createDecorationOptionXterm(code)
    }
    return window.createTextEditorDecorationType(dro)
}

/**
 * vt100 colors
 */
const VT100COLORS = {
    "0": "black",
    "5": "magenta",
    "1": "red",
    "6": "cyan",
    "2": "green",
    "7": "white",
    "3": "yellow",
    "8": "white",
    "4": "blue",
    "9": "black"
}

/**
 * 根据颜色代码生成 DecorationRenderOptions，vt100
 * @param {code} 颜色代码 
 * @returns DecorationRenderOptions
 */
function createDecorationOptionVt100(code) {
    var dt = {}
    var cs = code.split("")

    for (let i = 0; i < cs.length; i++) {
        switch (i) {
            case 1: // 前景色
                dt["color"] = VT100COLORS[cs[i > 7 ? 8 : i]]
                break;
            case 2: // 背景色
                dt["backgroundColor"] = VT100COLORS[cs[i > 7 ? 9 : i]]
                break;
            default:
                switch (cs[i]) {
                    case "1":
                        dt["fontWeight"] = "bold"
                        break;
                    case "3":
                        dt["italic"] = "true"
                        break;
                    case "4":
                        dt["underline"] = "true"
                        break;
                }
        }
    }
    return dt
}

/**
 * 根据颜色代码生成 DecorationRenderOptions，灰度
 * @param {code} 颜色代码 
 * @returns DecorationRenderOptions
 */
function createDecorationOptionGray(code) {
    let dt = {}
    let t = code.charAt(0)
    let cl = parseInt(code.substring(1))
    let g = cl * 255 / 24
    dt[t == "g" ? "color" : "backgroundColor"] = "rgb(" + g.toFixed(0) + "," + g.toFixed(0) + "," + g.toFixed(0) + ")"
    return dt
}


/**
 * 根据颜色代码生成 DecorationRenderOptions，truecolor
 * @param {code} 颜色代码 
 * @returns DecorationRenderOptions
 */
function createDecorationOptionTrueColor(code) {
    let dt = {}
    let s = code.charAt(0)
    let cl = "#" + code.substring(1)
    dt[s == "f" || s == "F" ? "color" : "backgroundColor"] = cl
    return dt
}


/**
 * 根据颜色代码生成 DecorationRenderOptions，xterm
 * @param {code} 颜色代码 
 * @returns DecorationRenderOptions
 */
function createDecorationOptionXterm(code) {
    let dt = {  }
    let cs = code.split("")
    let r = "rgb("
    for (let c in cs) {
        if (r === "rgb(") {
            r += xtermColor(cs[c])
            continue
        }
        r += "," + xtermColor(cs[c])
    }
    r += ")"
    dt["color"] = r
    return dt
}

function xtermColor(x) {
    if (!x) {
        x = "f"
    }
    let n = x.toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0)
    return 255 * n / 5
}
